package com.gexin.controller.system.order;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.alibaba.fastjson.JSONObject;
import com.gexin.common.AppConst;
import com.gexin.common.WebConst;
import com.gexin.common.utils.ExportDocBeetl;
import com.gexin.common.utils.IdGenerateUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.controller.system.user.SystemConfigController;
import com.gexin.model.order.*;
import com.gexin.model.shop.ShopInfo;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.JsonKit;
import com.jfinal.log.Log;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 话题
 * @author 方俊逸
 *  2016-05-12
 *
 */

@ControllerBind(controllerKey = "/system/excel/order", viewPath = "WEB-INF/system/weixin")
public class orderExcelController extends SystemConfigController {
	private static Log log = Log.getLog(orderExcelController.class);
	public void index(){
		log.info("商家订单导表");
		JSONObject obj= JSONObject.parseObject(getPara("obj"));
		Order order=new Order();
		order.setState(obj.getInteger("state"));
		order.setOrderPlatform(obj.getInteger("orderPlatform"));
		order.setExpressReceiveName(obj.getString("expressReceiveName"));
		order.setId(obj.getString("id"));
		order.setExpressPhone(obj.getString("expressPhone"));
		order.put("product_id",obj.getInteger("productId"));
		List<Order> order_list = Order.dao.shangjia_excel(ShopInfo.shop_id, order,obj.getString("startTime"), obj.getString("endTime"),getpage(), WebConst.Page_Size);
		List<OrderListExcel> excel_list =new LinkedList<OrderListExcel>();
 		for (Order o:order_list) {
			String [] add_arr = o.getExpressAddress().split("-");
			OrderListExcel excel =new OrderListExcel();
			excel.setId(o.getId());
			excel.setAddr_prov(add_arr[0]);
			excel.setAddr_city(add_arr[1]);
			excel.setAddr_area(add_arr[2]);
			excel.setExpress_address(StringUtils.isEmpty(o.getExpressAddress())?"/":o.getExpressAddress());
			excel.setExpress_phone(StringUtils.isEmpty(o.getExpressPhone())?"/":o.getExpressPhone());
			excel.setExpress_zip_code(StringUtils.isEmpty(o.getExpressZipCode())?"/":o.getExpressZipCode());
			excel.setExpress_name(StringUtils.isEmpty(o.getExpressName())?"/":o.getExpressName());
			excel.setCreatetime(o.getCreatetime());
			excel.setBuytime(o.getCreatetime());
			String state_name=(String) AppConst.Procuct_State.get(o.getState()+"");
			excel.setState(StringUtils.isEmpty(state_name)?"/":state_name);
			String express_name=(String)AppConst.Express.get(o.getExpressName());
			excel.setExpress_name(StringUtils.isEmpty(express_name)?"/":express_name);
			excel.setExpress_no(StringUtils.isEmpty(o.getExpressNo())?"/":o.getExpressNo());
			excel.setAdmin_remark(StringUtils.isEmpty(o.getAdminRemark())?"/":o.getAdminRemark());
			excel.setRemarks(StringUtils.isEmpty(o.getUserRemarks())?"/":o.getUserRemarks());

			List<OrderItemExcel> item_excel_list =new ArrayList<>();
			List<OrderItem> item_list = OrderItem.dao.getOrderItemsByOrderId(o.getId());
			for(OrderItem item:item_list){
				OrderItemExcel itemExcel=new OrderItemExcel();
				itemExcel.setNums(item.getNums());
				itemExcel.setProduct_name(item.getStr("product_name"));
				itemExcel.setId(o.getId());
				itemExcel.setSku_text(StringUtils.isEmpty(item.getSkuText())?"/":item.getSkuText());
				item_excel_list.add(itemExcel);
			}
			excel.setProducts(item_excel_list);
			excel_list.add(excel);
			excel.setTotal_amount(o.getTotalAmount().toString());
			excel.setExpress_receive_name(StringUtils.isEmpty(o.getExpressReceiveName())?"/":o.getExpressReceiveName());
			/*o.put("item", item_list);
			o.put("addr_prov",add_arr[0]);
			o.put("addr_city",add_arr[1]);
			o.put("addr_area",add_arr[2]);
			o.put"addr_detail",add_arr[3]);*/
		}
		System.err.println(JsonKit.toJson(excel_list));
 		try {
			Workbook workbook =  ExcelExportUtil.exportExcel(new ExportParams("订单导出",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),"sheet1"),OrderListExcel.class, excel_list);
			getResponse().setHeader("Content-Disposition", "attachment; filename=" + IdGenerateUtils.getId()+ ".xls");
			workbook.write(getResponse().getOutputStream());
		}catch (Exception e){
 			log.error("导出失败，"+e);
		}

		renderNull();
	}
	
	
	
	/**
	 * 订单详细列表  导出
	 * 
	 */
	public void detail(){
		log.info("商家订单导表");
		ExportDocBeetl export  = new ExportDocBeetl("/excel/shangjia_order_detail_list.xml");
		// 首先取到所以的订单状态
		Order order = getModel(Order.class, "order");
	    order.put("product_id", getParaToInt("product_id",null));
		setAttr("order", order);
		// 如果有状态 ,就返给前端 ,设置activity
		List<OrderState> state_list = OrderState.dao.cache_GetStateList();
		setAttr("state_list", state_list);
		// 根据shop_id 取到所有的大订单 和小订单
		List<Order> order_list = Order.dao.shangjia_excel(ShopInfo.shop_id, order,getPara("start_time", null), getPara("end_time", null),getpage(), WebConst.Page_Size);
		for (int i = 0; i < order_list.size(); i++) {
			Order o =  order_list.get(i);
			List<OrderItem> item_list = OrderItem.dao.getOrderItemsByOrderId(o .getId());
			o.put("item", item_list);
			if(!StringUtil.isBlank(o.getExpressAddress())){
				String str[] =o.getExpressAddress().split("-");
				o.put("str1", str[0]);
				o.put("str2", str[1]);
				o.put("str3", str[2]);
				o.put("str4", str[3]);
			}else{
				o.put("str1", "无");
				o.put("str2",  "无");
				o.put("str3", "无");
				o.put("str4",  "无");
			}
		}
		//export.binding("list", User.dao.out_excel(user));//绑定数据 在xml  里面直接拿
		export.binding("list", order_list);//绑定数据 在xml  里面直接拿
		export.exportDocument(getResponse(), getRequest(), IdGenerateUtils.getId());
		renderNull();
	}
	
	
	
	/**
	 *  维权订单  导出
	 * 
	 * fang  2016-08-26  
	 * 
	 */
	public void refund_order(){
		log.info("商家订单导表");
		ExportDocBeetl export  = new ExportDocBeetl("/excel/shangjia_refund_order_list.xml");
		// 首先取到所以的订单状态
		Order order = getModel(Order.class, "order");
		setAttr("order", order);
		// 如果有状态 ,就返给前端 ,设置activity
		List<OrderState> state_list = OrderState.dao.cache_GetStateList();
		setAttr("state_list", state_list);
		// 根据shop_id 取到所有的大订单 和小订单
		List<Order> order_list = Order.dao.shangjia_refund_excel(ShopInfo.shop_id, order,getPara("start_time", null), getPara("end_time", null),getpage(), WebConst.Page_Size);
		for (int i = 0; i < order_list.size(); i++) {
			List<OrderItem> item_list = OrderItem.dao.getOrderItemsByOrderId(order_list.get(i).getId());
			order_list.get(i).put("item", item_list);
		}
		//export.binding("list", User.dao.out_excel(user));//绑定数据 在xml  里面直接拿
		export.binding("list", order_list);//绑定数据 在xml  里面直接拿
		export.exportDocument(getResponse(), getRequest(), IdGenerateUtils.getId());
		renderNull();

	}
	
	/**
	 * 退款订单详情 
	 * fang 2016-08-26
	 */
	
	
	
}


